package bricolot.importer;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public abstract class AXmlImporter {

	protected DocumentBuilder mBuilder;

	protected Element mRootNode;

	protected File mXmlFile;

	protected boolean importFile(String aFileName) {
		boolean dataImported = true;

		try {
			this.loadFile(aFileName);
			this.parseFile();
		} catch (ImportException e) {
			e.printStackTrace();
			dataImported = false;
		}

		return dataImported;
	}

	protected void loadFile(String aFileName) throws ImportException {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			this.mBuilder = factory.newDocumentBuilder();
		} catch (Exception e) {
			throw new ImportException(e);
		}
		mXmlFile = new File(aFileName);
	}

	protected void parseFile() throws ImportException {

		try {
			Document document = this.mBuilder.parse(this.mXmlFile);
			this.mRootNode = document.getDocumentElement();
		} catch (Exception e) {
			throw new ImportException(e);
		}
	}

	public abstract Object loadContentsOf(String aFileName);

}
